package org.zhi

import org.apache.spark.ml.stat.Correlation
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.{Row, SparkSession}

import scala.swing.model.Matrix

/**
 * @className: SparkTest
 * @author: Administrator
 * @date: 2025-02-13 17:33
 */
object SparkTest {

  def main(args: Array[String]): Unit = {
    val data = Seq(
      Vectors.sparse(4, Seq((0, 1.0), (3, -2.0))),
      Vectors.dense(4.0, 5.0, 0.0, 3.0),
      Vectors.dense(6.0, 7.0, 0.0, 8.0),
      Vectors.sparse(4, Seq((0, 9.0), (3, 1.0)))
    )
    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .master("local[*]").getOrCreate()
    //导入隐式转换
    import spark.implicits._

    val df = data.map(Tuple1.apply).toDF("features")
    df.collect().foreach(println)
    val Row(coeff1: Matrix) = Correlation.corr(df,"features").head
    println(s"Pearson correlation matrix:\n $coeff1")

    val Row(coeff2: Matrix) = Correlation.corr(df,"features","spearman").head
    println(s"Spearman correlation matrix:\n $coeff2")

    spark.stop()
  }

}
